package com.boomsecret.juc.collections;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/23.
 */
public class LinkedTransferQueueDemo {
    private static LinkedTransferQueue<String> linkedTransferQueue = new LinkedTransferQueue<>();

    public static void main(String[] args) throws Exception {
        Thread threadA = new Thread(() -> {
            try {
                System.out.println("threadA start");
                System.out.println(linkedTransferQueue.take());
                System.out.println("threadA end");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        Thread threadB = new Thread(() -> {
            try {
                System.out.println("threadB start");
                System.out.println(linkedTransferQueue.take());
                System.out.println("threadB end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        threadA.start();
        threadB.start();

        TimeUnit.SECONDS.sleep(2);
        System.out.println(linkedTransferQueue.getWaitingConsumerCount());
        System.out.println(linkedTransferQueue.hasWaitingConsumer());

        linkedTransferQueue.tryTransfer("aa");
        linkedTransferQueue.tryTransfer("bb");
        TimeUnit.SECONDS.sleep(2);

        System.out.println("main end");
    }
}
